package handler

import (
	"fmt"
	"gin_maker_chat/model"
)

// 客户端维护的map
// var onlineUsers map[string]*model.User = make(map[string]*model.User, 10)

var (
	UserMgr *UserClientMgr
)

type UserClientMgr struct {
	OnlineUsers map[string]*model.User
}

// 完成对UserMgr的初始化工作
func init() {
	UserMgr = &UserClientMgr{
		OnlineUsers: make(map[string]*model.User, 10),
	}
}

func (this *UserClientMgr) ShowOnlineUsers() (users []*model.User) {
	fmt.Println("当前在线的用户列表")
	users = make([]*model.User, 10)
	userList := make([]model.User, 10)
	for _, user := range this.OnlineUsers {
		fmt.Println("用户名===", user.UserName)
		u := model.User{
			UserName:   user.UserName,
			UserStatus: user.UserStatus,
		}
		userList = append(userList, u)
		users = append(users, user)
	}
	fmt.Println("当前在线的用户users信息：", users)
	fmt.Println("当前在线的用户userList信息：", userList)
	return users
}

// 处理返回的通知在线用户消息
func (this *UserClientMgr) UpdateUserOnlineStatus(notifyUserStatusMsg *model.NotifyUserStatusMsg) {
	user, ok := this.OnlineUsers[notifyUserStatusMsg.UserName]
	if !ok {
		//该用户不存在
		user = &model.User{
			UserName: notifyUserStatusMsg.UserName,
		}
	}
	user.UserStatus = notifyUserStatusMsg.Status
	this.OnlineUsers[notifyUserStatusMsg.UserName] = user

	//更新完后，展示在线用户信息
	fmt.Println("======展示在线用户信息======")
	this.ShowOnlineUsers()
}
